//
// Created by DELL on 2022/4/18.
//

#include "Utils.h"

#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

namespace math
{
    Mat4 Identity{1.0f};
    Vec3 X_Axis{1, 0, 0};
    Vec3 Y_Axis{0, 1, 0};
    Vec3 Z_Axis{0, 0, 1};

    inline float printNumber(float f)
    {
        if (std::abs(f) < 1e-6 )
        {
            return 0;
        }
        return f;
    }

    void dump(const Mat4& m, const char* name /*= ""*/)
    {
        std::cout<< name << " Mat4:\n ";
        for (int i = 0; i < 4; ++i) {
            std::cout << "    ";
            for (int j = 0; j < 4; ++j) {
                std::cout << printNumber(m[j][i]) << ", ";
            }
            std::cout<<'\n';
        }
    }

    void dump(const Mat3& m, const char* name /*= ""*/)
    {
        std::cout<< name << " Mat3:\n ";
        for (int i = 0; i < 3; ++i) {
            std::cout << "    ";
            for (int j = 0; j < 3; ++j) {
                std::cout << printNumber(m[j][i]) << ", ";
            }
            std::cout<<'\n';
        }
    }

    void dump(const Vec2& v, const char* name /*= ""*/)
    {
        std::cout<< name << " Vec2("<<printNumber(v.x) << ", "<<printNumber(v.y) << ")\n";
    }

    void dump(const Vec3& v, const char* name /*= ""*/)
    {
        std::cout<< name << " Vec3("<<printNumber(v.x) << ", "<<printNumber(v.y) << ", " <<printNumber(v.z) << ")\n";
    }

    void dump(const Vec4& v, const char* name /*= ""*/)
    {
        std::cout<< name << " Vec4("<<printNumber(v.x) << ", "<<printNumber(v.y) << ", "
                  <<printNumber(v.z) << ", "<<printNumber(v.w) << ")\n";
    }

}// namespace math

BEGIN_NS_VULKAN


END_NS_VULKAN


std::string readFileToString(const std::string& filePath)
{
    std::string text;
    std::ifstream stream(filePath, std::ios::binary | std::ios::in);
    if (stream.is_open())
    {
        std::streampos start = stream.tellg();
        stream.seekg(0, std::ios::end);
        std::streampos end = stream.tellg();
        stream.seekg(0, std::ios::beg);
        auto fileSize = end - start;
        text.resize(uint32_t(fileSize));
        stream.read(text.data(), fileSize);
        stream.close();
    }
    else
    {
        std::string msg = "failed to open file!, File Path: " + filePath;
        LOGE(msg);
        throw std::runtime_error(msg);
    }
    return text;
}

ByteBuffer readFile(const std::string& filePath)
{
    std::ifstream file(filePath, std::ios::ate | std::ios::binary);

    if (!file.is_open())
    {
        std::string msg = "failed to open file!, File Path: " + filePath;
        LOGE(msg);
        throw std::runtime_error(msg);
    }
    auto fileSize = file.tellg();
    ByteBuffer buffer(fileSize);
    file.seekg(0);
    file.read(buffer.data(), fileSize);
    file.close();

    return buffer;
}
